﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyWentWhere.DataModel.Collections
{
    interface ITable<T>
    {
        /// <summary>
        /// Get a value, indexed by row and col.
        /// If the cell is empty, returns default(T).
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        /// <returns></returns>
        T this[int rowIndex, int colIndex] { get; set; }

        int RowCount { get; }
        int ColumnCount { get; }
    }

    static class TableExtensionMethods
    {
        public static String ToCsvString<T>(this ITable<T> table)
        {
            StringBuilder sb = new StringBuilder();
            for (int row = 0; row < table.RowCount; row++)
            {
                for (int col = 0; col < table.ColumnCount; col++)
                {
                    String field = table[row, col].ToCsvEscapedString();
                    sb.Append(field);
                    sb.Append(",");
                }
                sb.AppendLine();
            }

            return sb.ToString();
        }

    }

}
